package com.gxa.insurance.controller;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.pojo.Banner;
import com.gxa.insurance.service.BannerService;
import com.gxa.insurance.util.JsonResult;
import com.gxa.insurance.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Min
 */
@CrossOrigin
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService service;

    @GetMapping("/indexAll")
    @ApiOperation("首页查询所有banner")
    public Map selectAllByPage(@RequestParam("page")String page, @RequestParam("pageSize")String pageSize) throws BusinessException {
        if(StringUtil.isEmpty(page) || StringUtil.isEmpty(pageSize)|| !StringUtil.isNumber(page) || !StringUtil.isNumber(pageSize))
        {
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        Map<String,Object> map=service.selectAllByPage(page,pageSize);
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("1");
        jsonResult.setData(map.get("list"));
        jsonResult.put("count",map.get("count"));
        return jsonResult.getMap();
    }

    @ApiOperation(value = "banner添加数据",produces = "body")
    @PostMapping("/add")
    public Map addBanner(@RequestBody Banner banner)throws Exception{
        if(banner==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        Boolean isOk=service.addBanner(banner);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("删除banner的操作")
    @GetMapping("/del")
    public Map deleteBanner(@RequestParam("id")String id, @ApiIgnore HttpServletRequest request) throws BusinessException {
        if(StringUtil.isEmpty(id) || !StringUtil.isNumber(id)){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();

        Boolean isOk=service.deleteBanner(id,request);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("设置优先级 移动")
    @ApiImplicitParams({
            @ApiImplicitParam(name="exId1",value="exId1 要交换的banner第一个id"),
            @ApiImplicitParam(name = "exId2",value = "exId2 要交换的banner的第二个id")
    })
    @GetMapping("/move")
    public Map priorityMoving(@RequestParam("exId1")String id1,@RequestParam("exId2")String id2) throws BusinessException {
        if(StringUtil.isEmpty(id1) || StringUtil.isEmpty(id2)){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.exchangePriority(id1,id2);
        JsonResult jsonResult = new JsonResult();
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("优先级修改成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("优先级修改失败");
        }
        return jsonResult.getMap();
    }


    @ApiOperation("更新banner")
    @PostMapping("/update")
    public Map updateBanner(@RequestBody Banner banner) throws BusinessException {
        if(banner==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        Boolean isOk=service.updateById(banner);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("更新是否可见的操作")
    @GetMapping("/updateEnable")
    public Map updateIsEnable(@RequestParam("id")Integer id,@RequestParam("isEnable")Boolean isEnable) throws BusinessException {
        if(id==null || id<=0 || isEnable==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        Boolean isOk=service.updateEnableById(id,isEnable);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("通过banner顺序字段查找id")
    @GetMapping("/getBanId")
    public Map selectID(@RequestParam("prior")Integer prior) throws BusinessException {
        if(prior<=0 || prior==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        Integer id=service.selectID(prior);
        if(id==null|| id<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(id);
        }
        return jsonResult.getMap();
    }
}
